/* 
 * File:   SharedMemory.cpp
 * Author: huzeyfe
 * 
 * Created on 8. Dezember 2011, 07:23
 */

#include "SharedMemory.h"

int buffer = 0;

using namespace std;

SharedMemory::SharedMemory(int size) {
    array = new int[size + 1];
    buffer = size + 1;
    l = 0;
    u = 0;
}

SharedMemory::~SharedMemory() {
    delete [] array;
}

void SharedMemory::enter(int wert) {
    if((buffer - l + u) % buffer == buffer - 1) {
        overflow = true;
        printf("Queue overflow!\n");
    } else {
        array[u] = wert;
        printf("%i wurde hinzugefügt!\n" , array[u]);
        u = (u + 1) % buffer;
    }
}

int SharedMemory::leave() {
    if(isEmpty()) {
        overflow = false;
        printf("Queue underflow!\n");
        return -1;
    } else {
        int i = array[l];
        printf("%i wurde gelöscht!\n", array[l]);
        array[l] = -1;
        l = (l + 1) % buffer;
        return i;
    }
}

bool SharedMemory::isEmpty() {
    return l == u;
}